package com.cls.business.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.business.entity.ApplicantInfo;
import com.cls.business.service.IApplicantInfoService;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.MyConstant;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.common.exception.MyException;
import com.cls.common.utils.MyUtil;
import com.github.pagehelper.PageInfo;
import com.wuwenze.poi.ExcelKit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 申请人信息表 Controller
 *
 * @author wandering
 * @date 2020-11-11 18:15:13
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class ApplicantInfoController extends BaseController {

    private final IApplicantInfoService applicantInfoService;

    /**
     * 申请人信息表列表页面
     **/
    @GetMapping(MyConstant.VIEW_PREFIX + "applicantInfo")
    public String applicantInfoIndex() {
        return MyUtil.view("applicantInfo/applicantInfo");
    }

    /**
     * 申请人信息表新增页面
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/applicantInfo/applicantInfoAdd")
    public String applicantInfoAdd(Model model) {
        return MyUtil.view("business/applicantInfo/applicantInfoAdd");
    }

    /**
     * 申请人信息表修改页面
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/applicantInfo/applicantInfoUpdate/{applicantInfoId}")
    public String applicantInfoUpdate(@PathVariable Long applicantInfoId, Model model) {
        ApplicantInfo applicantInfo = applicantInfoService.getById(applicantInfoId);
        model.addAttribute("applicantInfo", applicantInfo);
        return MyUtil.view("business/applicantInfo/applicantInfoUpdate");
    }


    /**
     * 通知公告详情页面
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/applicantInfo/applicantInfoDetail/{applicantInfoId}")
    public String applicantInfoDetail(@PathVariable Long applicantInfoId, Model model) {
        ApplicantInfo applicantInfo = applicantInfoService.getById(applicantInfoId);
        model.addAttribute("applicantInfo", applicantInfo);
        return MyUtil.view("business/applicantInfo/applicantInfoDetail");
    }

    @GetMapping("applicantInfo")
    @ResponseBody
    @RequiresPermissions("applicantInfo:list")
    public ResponseVO getAllApplicantInfos(ApplicantInfo applicantInfo) {
        return ResponseVO.success(applicantInfoService.findApplicantInfos(applicantInfo));
    }

    @GetMapping("applicantInfo/list")
    @ResponseBody
    @RequiresPermissions("applicantInfo:list")
    public ResponseVO applicantInfoList(QueryRequest request, ApplicantInfo applicantInfo) {
        PageInfo<ApplicantInfo> pageInfo = this.applicantInfoService.findApplicantInfos(request, applicantInfo);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增ApplicantInfo", exceptionMessage = "新增ApplicantInfo失败")
    @PostMapping("applicantInfo")
    @ResponseBody
    @RequiresPermissions("applicantInfo:add")
    public ResponseVO addApplicantInfo(@Valid ApplicantInfo applicantInfo) {
        this.applicantInfoService.createApplicantInfo(applicantInfo);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除ApplicantInfo", exceptionMessage = "删除ApplicantInfo失败")
    @PostMapping("applicantInfo/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("applicantInfo:delete")
    public ResponseVO deleteApplicantInfos(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.applicantInfoService.deleteApplicantInfos(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个ApplicantInfo", exceptionMessage = "删除ApplicantInfo失败")
    @PostMapping("applicantInfo/delete/{applicantInfoId}")
    @ResponseBody
    @RequiresPermissions("applicantInfo:delete")
    public ResponseVO deleteApplicantInfo(@PathVariable Long applicantInfoId) {
        this.applicantInfoService.removeById(applicantInfoId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改ApplicantInfo", exceptionMessage = "修改ApplicantInfo失败")
    @PostMapping("applicantInfo/update/{applicantId}")
    @ResponseBody
//    @RequiresPermissions("applicantInfo:update")
    public ResponseVO updateApplicantInfo(@PathVariable Long applicantId, ApplicantInfo applicantInfo) {
        Subject subject = super.getSubject();
        if (!subject.hasRole(MyConstant.REGISTER_ROLE_NAME)) {
            throw new MyException("对不起，当前用户非申请人角色不允许修改！");
        }
        applicantInfo.setApplicantId(applicantId);
        applicantInfo.setEmail(null);
        /*String identity = applicantInfo.getIdentity();
        if(!StringUtils.isEmpty(identity) && identity.length() == 18){
            String birth =  identity.substring(6,14);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            try {
                applicantInfo.setBirthday(sdf.parse(birth));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }*/
        this.applicantInfoService.updateById(applicantInfo);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改ApplicantInfo", exceptionMessage = "导出Excel失败")
    @PostMapping("applicantInfo/excel")
    @ResponseBody
    @RequiresPermissions("applicantInfo:export")
    public void export(QueryRequest queryRequest, ApplicantInfo applicantInfo, HttpServletResponse response) {
        List<ApplicantInfo> applicantInfos = this.applicantInfoService.findApplicantInfos(queryRequest, applicantInfo).getList();
        ExcelKit.$Export(ApplicantInfo.class, response).downXlsx(applicantInfos, false);
    }

}
